package com.taokatao.cardmee.business.profile;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taokatao.cardmee.ActivityCollector;
import com.taokatao.cardmee.App;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.BusEvent;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.business.MainActivity;
import com.taokatao.cardmee.business.login.LoginActivity;
import com.taokatao.cardmee.common.widget.SwitchView;
import com.taokatao.cardmee.db.model.User;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class ProfileSettingsActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.v_push_able) SwitchView vPushAble;
    @BindView(R.id.t_logout) TextView tLogout;
    @BindView(R.id.i_load_push) ImageView iLoadPush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_settings);
        ButterKnife.bind(this);

        boolean isPushAble = Utils.spReadBoolean(C.sp.SETTINGS_PUSH_ABLE, true);
        vPushAble.setOpened(isPushAble);
        vPushAble.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean expectedResult = vPushAble.isOpened();
                if (expectedResult) {
                    JPushInterface.resumePush(getApplicationContext());
                } else {
                    JPushInterface.stopPush(getApplicationContext());
                }
                Utils.spWrite(C.sp.SETTINGS_PUSH_ABLE, expectedResult);
            }
        });
    }

    @Override
    @OnClick({R.id.t_logout})
    public void onClick(View v) {
        if (v == tLogout) {
            User currUser = App.i().getCurrUser();
            if (currUser != null) {
                Utils.spWrite(C.sp.LAST_LOGOUT_USER_TEL, currUser.getUserTel());
                currUser.setUserTel("");
                currUser.setHeadImgUrl("");
                currUser.setSex(User.GENDER_FEMALE);
                currUser.setUserName("");
                currUser.setRealName("");
                currUser.setIntegral(0);
                currUser.setCardCount(0);
                currUser.setBirthday(0L);
                App.i().setCurrUser(currUser);
            }
            Utils.postMainThreadBusEvent(BusEvent.USER_LOGOUT);

            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            ActivityCollector.finishAllExcept(MainActivity.class);
        }
    }
}
